﻿using System;
using System.Collections.Generic;
using System.Linq;
using MDT_MonitorModel;
using Windows.UI.Xaml.Controls;

// Pour en savoir plus sur le modèle d'élément Page Éléments groupés, consultez la page http://go.microsoft.com/fwlink/?LinkId=234231

namespace MDT_Metro_monitoring
{
    /// <summary>
    /// Page affichant une collection groupée d'éléments.
    /// </summary>
    public sealed partial class ComputerGroupedItems : MDT_Metro_monitoring.Common.LayoutAwarePage
    {
        public ComputerGroupedItems()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            loadComputers();
        }

        private async void loadComputers()
        {

            List<Computer> dictionary = new List<Computer>();
            foreach (Server srv in App.Servers)
            {
                if (srv.Online)
                {
                    srv.Clients = (List<Computer>)await srv.ClientData.LoadComputersAsync();
                    dictionary.AddRange(srv.Clients);
                }
            }
            //Groups all computer into one single group, instead of one group per computer
            //var result = from cp in dictionary group cp by true into grp orderby grp.Key select grp;
            var result = from comp in (from cp in dictionary orderby cp.PercentComplete descending select cp) group comp by comp.Status into grp select grp ;

            this.DefaultViewModel["Groups"] = result;
        }
    }
}
